package com.df.springmvc.system.manage.dao;

import java.util.ArrayList;
import java.util.HashMap;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.df.springmvc.supper.SupperDao;


@Repository
public class SearchDao extends SupperDao{
	private String message;
	
	/**
	 * *
	 * @param sql
	 *            查询的SQL
	 * @param param_HashMap
	 *            查询参数
	 * @param i
	 *            从第几天开始
	 * @param j
	 *            显示多少条数据
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Object[]> query(HashMap<String,String> map,Object[] attribute,int i,int j) throws Exception{
		// TODO Auto-generated method stub
		//1>根据登录账号查询出当前登录人的人员编号和工号
		Session session = getSessionFactory().getCurrentSession();
		String sql = "select  staffCode from b_staff_info where incumbencyStatus='1' and  userAccount=:userAccount ";
		SQLQuery query = session.createSQLQuery(sql);
		query.setString("userAccount", map.get("userId"));
		ArrayList<String> list = (ArrayList<String>) query.list();
		String staffCode = "";
		for (int k = 0; k < list.size(); k++) {
			staffCode=list.get(i);
		}
		//2>查询出当前登录员工参与过的项目
		Session session2=getSessionFactory().getCurrentSession();
		String sql2="select '' as 'ck',o.guid,o.projectCode,o.projectName,o.packetNum,(select exp.expTabName from  bm_business_info exp where exp.status='1' and exp.expTabCode=o.projectTypeCode) as projectType,(select exp.expTabName from  bm_business_info exp where exp.status='1' and exp.expTabCode=o.projectModeCode) as projectMode,batchCode,tenderComment,(select exp.expTabName from  bm_business_info exp where exp.status='1' and exp.expTabCode=o.projectStatusCode) as projectStatus,publishStatusCode,(select exp.expTabName from  sm_system_info exp where exp.status='1' and exp.expTabCode=o.publishStatusCode) as publishStatus,projectManager,(select sta.staffName from b_staff_info sta where o.projectManager=sta.userAccount ) as projectManagerName,projectAssistant,(select sta.staffName from b_staff_info sta where o.projectAssistant=sta.staffCode ) as projectAssistantName,date_format(o.createTime,'%Y-%m-%d') createTime  from b_project_info o inner join br_projectinfo_staff ps on ps.projectCode=o.projectCode   where 1=1 and o.status = 1 and ps.staffCode='"+staffCode+"'";
		if(map.get("searchInput") != null && !"".equals(map.get("searchInput"))){
			sql2 = sql2+" and (o.projectName LIKE :searchName or o.projectCode LIKE :searchCode ) ";
		}
		sql2 = sql2+" order by o.createTime desc";
		SQLQuery query2=session2.createSQLQuery(sql2);
		if(map.get("searchInput") != null && !"".equals(map.get("searchInput"))){
			query2.setParameter("searchCode","%" + map.get("searchInput") + "%");
			query2.setParameter("searchName","%" + map.get("searchInput") + "%");
		}
		query2.setFirstResult(i);
		query2.setMaxResults(j);
		ArrayList<Object[]> returnlist = (ArrayList<Object[]>) query2.list();
		for (int k = 0; k < returnlist.size(); k++) {
			Object[] o = (Object[])returnlist.get(k);
			int returnStatus=0;
			String publishCode=""+o[10];//项目状态
			String projectManager=""+o[12];//项目负责人
			//项目状态为未发布，草稿项目经理是当前登录人,跳转到修改项目信息页面，否则跳转到浏览页面
			if(("publish_0".equals(publishCode)||"publish_un".equals(publishCode))&& (projectManager.equals(map.get("userId")))){
				returnStatus=1;
			}
			String str = "<a href='javascript:void(0);' style='text-decoration: underline;' onclick=PortletClick('"+o[1]+"','"+returnStatus+"')> "+o[2]+"</a>"; 
			o[2] = str;
		}
		return returnlist;
	}
	
	public String getMessage(){
		return message;
	}
	public void setMessage(String message){
		this.message=message;
	}
}
